package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Gusetbook;
import com.foreknow.service.GuestbookeService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GusetbookeServiceInpl implements GuestbookeService {
    @Override
    public boolean save(Gusetbook gusetbook)  {
        DBManager db=DBManager.getInstance();
        Connection connection=db.getConnection();
        GuestbookImpl guestbookDao = new GuestbookImpl();
        //因为GusetbookDao集成了besedao,所以可以直接调用Basedao中的设置
        //数据库连接的方式
        guestbookDao.setConnection(connection);
        boolean isRight=false;
        try {
           isRight= guestbookDao.addMessage(gusetbook);
           //提交事物,这里一定要记得提交事物,因为数据库的事物只作用在增删改
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public List<Object> quertInfo() {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        GuestbookImpl guestbook = new GuestbookImpl();
        guestbook.setConnection(connection);
        List<Object>list=null;
        try {
           list= guestbook.getQueryAll();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteBean(Integer id) {
        DBManager db=DBManager.getInstance();
        Connection connection=db.getConnection();
        GuestbookImpl guestbookDao = new GuestbookImpl();
        //因为GusetbookDao集成了besedao,所以可以直接调用Basedao中的设置
        //数据库连接的方式
        guestbookDao.setConnection(connection);
        boolean isRight=false;
        try {
            isRight= guestbookDao.deleteById(id);
            //提交事物,这里一定要记得提交事物,因为数据库的事物只作用在增删改
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public Gusetbook getById(Integer id) {
        DBManager db = DBManager.getInstance();
        Connection conn = db.getConnection();
        GuestbookImpl guestbook = new GuestbookImpl();
        guestbook.setConnection(conn);
        Gusetbook guset=null;
        try {
           guset = guestbook.queryById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return guset;
    }

    @Override
    public int updateUser(int id, Gusetbook gusetbook) {
        DBManager db = DBManager.getInstance();
        Connection conn = db.getConnection();
        GuestbookImpl gb = new GuestbookImpl();
        int i=0;
        try {
             i = gb.updateById(id, gusetbook, conn);
             conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        return i;
    }
}



